<template>
  <div id="user-login">
    <el-container>
      <el-header>
        <img class="img3" src="../assets/img/3.png">
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input type="text" v-model="ruleForm.username"></el-input>{{ruleForm.username}}
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>{{ruleForm.password}}
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">注册</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    
  </div>
  
</template>

<script>
export default {
  name: "Login",
  props:[''],
  data() {
    return {
      ruleForm: {
        username: 'markerhub',
        password: '111111',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        const _this = this;//避免下面使用this是this.$axios
        if (valid) {
          //url结合了axios中的url
          this.$axios.post('/login',this.ruleForm).then(res=>{
            // const tokenObject=JSON.stringify( res.headers); //这是获得请求头对象
            const token1=res.headers;
            //特别注意前端是小写的，后端是大写的
            const token=res.headers['authorization']; //这是获得请求头中的Authorization的value值，
            const userinfo = res.data.data;
            console.log(token1)
            console.log(token)
            console.log("userinfo:"+userinfo)
            //调用state的commit就是使用mutations中的方法
            _this.$store.commit("SET_TOKEN",token);
            _this.$store.commit("SET_USERINFO",userinfo);
            //验证是否可以获取
            console.log(_this.$store.getters.getToken);
            console.log(_this.$store.getters.getUserInfo);
            //登录后跳转到
            _this.$router.push('/blog/all')
           
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>

<style scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 160px;
}
.img3{
  height: 100%;
  width: 100%;
  margin-top: 1px;
}
.login-form{
  
}
.demo-ruleForm{
  width: 400px;
  margin: 0 auto;
}

</style>